package edu.njfu.sas.servlet;

import com.google.gson.Gson;
import edu.njfu.sas.dao.impl.UserDapImpl;
import edu.njfu.sas.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

@WebServlet("/userRegister")
public class  RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String userName=request.getParameter("username");
        String password=request.getParameter("password");
        User user=new User(userName,password);
        UserDapImpl userDap=new UserDapImpl();
        boolean result=userDap.registerUser(user);
        HashMap<String,String> map=new HashMap<String, String>();
        if(result){
            map.put("result","注册成功");
            map.put("msg","ok");
        }else{
            map.put("result","注册失败");
            map.put("msg","error");
        }

        PrintWriter out=response.getWriter();
        out.write(new Gson().toJson(map));


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
